import 'package:PollOnLine/utils/utils.dart';
import 'package:PollOnLine/values/MyColors.dart';
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'main_logic.dart';

class MainPage extends StatefulWidget {
  final String title;

  const MainPage({Key? key, required this.title}) : super(key: key);

  @override
  State<MainPage> createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  final logic = Get.put(MainLogic());
  final state = Get.find<MainLogic>().state;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MainLogic>(builder: (logic) {
      return PopScope(
        canPop: false,
        onPopInvoked: (bool didPop) {
          // 当 canPop 为 false，则执行系统返回时会被拦截，并且调用 onPopInvoked 方法，同时 didPop 为 false，
          // 此时进行逻辑判断，
          // 如果需要返回则执行 Navigator.of(context).pop(); 注意此时 onPopInvoked 又会被调用，并且 didPop 为 true。
          if (!didPop) {
            logic.onPopInvoked();
          }
        },
        child: Scaffold(
          backgroundColor: MyColors.FFEDF1F5,
          body: IndexedStack(index: state.currentIndex, children: state.pages),
          bottomNavigationBar: BrnBottomTabBar(
            // 选中item后的颜色
            currentIndex: state.currentIndex,
            onTap: logic.onItemSelected,
            items: <BrnBottomTabBarItem>[
              for (final tab in state.tabs)
                BrnBottomTabBarItem(
                  backgroundColor: Colors.transparent,
                  // 	tab 选中文本样式
                  selectedTextStyle: TextStyle(color: MyColors.FF1060DD, fontSize: 11.r),
                  // 	tab 未选中文本样式
                  unSelectedTextStyle: TextStyle(color: MyColors.FF333333, fontSize: 11.r),
                  icon: Image(image: AssetImage(tab.icon), width: 13.5.r),
                  activeIcon: Image(image: AssetImage(tab.activeIcon), width: 13.5.r),
                  title: Text(tab.title),
                ),
            ],
          ),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<MainLogic>();
    super.dispose();
  }
}
